﻿using System;
using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit;
using System.Collections.Generic;

namespace CollegeShop.Repository.Win.UserControl
{
    #region RichEdit
    public partial class RichEdit : CollegeShop.Repository.Win.UserControl.TutorialControl
    {

        public RichEdit() {
            InitializeComponent();
            RtfLoadHelper.Load("TextWithImages.rtf", rich);
            rich.Focus();
            new RichEditDemoExceptionsHandler(rich).Install();
        }

        #region Properties
        public override RichEditControl PrintingRichEditControl { get { return rich; } }
        public bool IsReloadDocument { get { return true; } }
        public string CurrentFileName { get { return rich.Options.DocumentSaveOptions.CurrentFileName; } }
        #endregion

        protected override void DoShow() {
            base.DoShow();
            rich.Focus();
        }

        #region Hendlers
        void OnAllowCharacterFormatting(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.CharacterFormatting = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowCharacterStyle(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.CharacterStyle = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowParagraph(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Paragraphs = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowParagraphPraperties(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.ParagraphFormatting = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowParagraphStyle(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.ParagraphStyle = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowParagraphTabs(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.ParagraphTabs = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowHyperlinks(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Hyperlinks = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowBookmarks(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Bookmarks = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowBulletNumbering(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Numbering.Bulleted = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowSimpleNumbering(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Numbering.Simple = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowMultiLevelNumbering(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Numbering.MultiLevel = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowPicture(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.InlinePictures = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowSymbolTabs(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.TabSymbol = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowSections(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Sections = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnAllowHeadersFooters(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.HeadersFooters = GetOptionValue(sender);
            ReloadDocument();
        }
        void OnApplyRestrictions(object sender, EventArgs e) {
            ReloadDocument();
        }
        void OnEdtAllowTables(object sender, EventArgs e) {
            rich.Options.DocumentCapabilities.Tables = GetOptionValue(sender);
            ReloadDocument();
        }
        #endregion

        void ReloadDocument() {
            if (IsReloadDocument && !String.IsNullOrEmpty(CurrentFileName))
                rich.LoadDocument(CurrentFileName);
        }
        DocumentCapability GetOptionValue(object sender) {
            if (((CheckEdit)sender).Checked)
                return DocumentCapability.Enabled;
            return DocumentCapability.Disabled;
        }

        private void rich_TextChanged(object sender, EventArgs e)
        {
            // = this.rich.HtmlText;
            List<LinkedAttachementInfo> linkList = new List<LinkedAttachementInfo>();
            RichExporter exporter = new RichExporter(this.rich);
            string html = "";
            exporter.Export(out html, out linkList);
        }
    }
    #endregion
}
